<template>
  <div class="warp-box" id="upadd">
    <el-row class="search-box">
      <el-col :span="24" class="add-bottom">
        <span class="personal-title" v-text="parames.id?'编辑页面':'新建页面'"></span>
      </el-col>
    </el-row>
    <div class="main-box">
      <div class="inforlist">
        <div class="inforadd" style="margin-top: 30px;width: 880px">
          <el-form
            :model="parames"
            :rules="rules"
            ref="parames"
            label-width="120px"
            class="demo-ruleForm"
          >
            <el-form-item label="名称:" prop="name">
              <el-input style="width:400px;" v-model="parames.name" placeholder="请输入名称"></el-input>
            </el-form-item>
            <el-form-item label="跳转策略:" prop="type">
              <el-popover
                placement="right"
                title
                offset="300"
                width="200"
                trigger="manual"
                v-model="typeBtn"
                :content="content"
              ></el-popover>
              <el-switch
                style="margin-left: -50px"
                class="switch1"
                v-model="parames.type"
                active-value="1"
                inactive-value="0"
                active-text="按权重"
                inactive-text="按顺序"
                @change="changeType"
              ></el-switch>
            </el-form-item>

            <el-form-item label="状态:" prop="status">
              <el-popover
                placement="right"
                title
                offset="300"
                width="200"
                trigger="manual"
                v-model="statusBtn"
                content="温馨提示：选择下线后应用就不会显示在前端界面上！"
              ></el-popover>
              <el-switch
                v-model="parames.status"
                active-value="1"
                inactive-value="0"
                active-text="上线"
                inactive-text="下线"
                @change="changeStatus"
              ></el-switch>
            </el-form-item>

            <el-form-item label="描述:" prop="memo">
              <el-input
                rows="6"
                style="width:400px;"
                type="textarea"
                v-model="parames.memo"
                placeholder="请输入描述"
              ></el-input>
            </el-form-item>
            <el-form-item>
              <el-button round type="primary" @click="handleSend">保存并返回列表</el-button>
              <el-button round @click="resetData">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      parames: {
        id: "",
        name: "",
        status: "0",
        type: "0",
        memo: ""
      },
      typeBtn: false,
      statusBtn: false,
      content: '',
      moduleDialog: {
        type: "",
        title: "",
        show: false
      },
      rules: {
        name: [{ required: true, message: "请输入名称", trigger: "blur" }]
      }
    };
  },
  methods: {
    changeType(val) {
      this.typeBtn = true;

      if (val == 0) {
        this.content = '温馨提示：使用自动跳转链接，将按照链接的顺序依次跳转！'
      } else {
        this.content = '温馨提示：使用自动跳转链接，将按照权重随机跳转！'
      }
      setTimeout(() => {
          this.typeBtn = false;
        }, 1500);
    },
    changeStatus(val) {
      if (val == 0) {
        this.statusBtn = true;
        setTimeout(() => {
          this.statusBtn = false;
        }, 1000);
      }
    },
    handleSend() {
      let url = "";
      if (this.parames.id) {
        url = "tx/site/edit";
      } else {
        url = "tx/site/save";
      }
      this.$refs.parames.validate(valid => {
        if (valid) {
          this.$axios
            .post(url, {
              id: this.parames.id,
              name: this.parames.name,
              status: this.parames.status,
              type: this.parames.type,
              memo: this.parames.memo
            })
            .then(res => {
              if (res.code === 10200) {
                this.resetData();
                this.$router.push({ name: "my_page_list" });
              }
            });
        } else {
          return false;
        }
      });
    },
    render(id) {
      this.$axios.get("tx/site/edit/" + id).then(res => {
        if (res.code === 10200) {
          if (id === "0") {
            // this.parames.modules = res.data.modules;
          } else {
            this.parames = res.data;
            this.parames.status = res.data.status + "";
            this.parames.type = res.data.type + "";
          }
        }
      });
    },
    resetData() {
      this.parames.id = "";
      this.parames.name = "";
      this.parames.status = "0";
      this.parames.type = "0";
      this.parames.memo = "";
    }
  },
  mounted() {
    this.parames.id = this.$route.query.id;
    if (this.parames.id) {
      this.render(this.parames.id);
    } else {
      //   this.render("0");
    }
  }
};
</script>
<style>
.el-checkbox + .el-checkbox {
  margin: 0;
  padding-right: 30px;
}
</style>
